//
//  ShowView.m
//  wnkdwaiter
//
//  Created by 尹桥印 on 15/3/28.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "CustomShowView.h"

@interface CustomShowView()
@property (strong, nonatomic) UIButton *bgBtn;//背景的btn，点击则把此view消失在屏幕上
@property (strong, nonatomic) UIView *customView;
@end

@implementation CustomShowView


-(instancetype)init{
    self = [super init];
    if (self) {
        [self setSize:CGSizeMake(SCREENW, SCREENH)];
        _bgBtn = [[UIButton alloc]initWithFrame:self.frame];
        [_bgBtn setBackgroundColor:[UIColor blackColor]];
        [_bgBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
        [_bgBtn setAlpha:0.3];
        [self addSubview:_bgBtn];
    }
    return self;
}

-(instancetype)initWithCustomView:(UIView *)view point:(CGPoint)point{
    self = [self init];
    if (self) {
        view.x = point.x;
        view.y = point.y;
        _customView = view;
        [self addSubview:view];
    }
    return self;
}

-(instancetype)initWithCustomView:(UIView *)view location:(CustomShowViewLocationType)location
{
    self = [self init];
    if (self) {
        if(location == CustomShowViewLocationBottomType){
            CGFloat marginTop =  self.height-view.height;
            view.x = 0;
            view.y = marginTop;
        }else if(location == CustomShowViewLocationTopType){
            view.x = 0;
            view.y = 0;
        }else{
            view.center = self.center;
        }
        _customView = view;
        [self addSubview:view];
    }
    return self;
}

-(void)showWithAnim:(BOOL)animated isTransparent:(BOOL)isTransparent{
    [self showWithAnim:YES];
    [self.bgBtn setAlpha:0.0];
}

-(void)showWithAnim:(BOOL)animated
{
    if (!self.superview) {
        //添加键盘弹出与隐藏的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        
        [_bgBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];

        if (animated) {
            self.alpha = 0.001;
            [[UIApplication sharedApplication].keyWindow addSubview:self];
            [UIView animateWithDuration:0.25 animations:^{
                self.alpha = 1;
            }];
        }else{
            [[UIApplication sharedApplication].keyWindow addSubview:self];
        }
        
        if([_delegate respondsToSelector:@selector(showWithCallBack)]){
            [_delegate showWithCallBack];
        }
    }
}

-(void)hideWithAnim:(BOOL)animated
{
    //移除键盘监听
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
    [_bgBtn removeTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    
    if (animated) {
        [UIView animateWithDuration:0.25 animations:^{
            [self setAlpha:0.001];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }else{
        [self removeFromSuperview];
    }
    
    if([_delegate respondsToSelector:@selector(hideWithCallBack)]){
        [_delegate hideWithCallBack];
    }
}

- (void)keyboardWillShow:(NSNotification *)notif
{
    //在键盘显示的时候去调整View
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    //首页判断键盘有没有遮挡View，如果没有遮挡，那个什么也不做
    CGFloat viewMaxY = CGRectGetMaxY(_customView.frame);
    if (viewMaxY+SCALEX(10)>SCREENH-keyboardSize.height) {
        //代表键盘遮挡了view
        //需要将view移动
        [UIView animateWithDuration:0.25 animations:^{
            [_customView setY:SCREENH-keyboardSize.height-_customView.height-SCALEX(10)];
        }];
    }
}

-(void)keyboardWillHide:(NSNotification*)notif
{
    //在键盘隐藏的时候去调整View
    [UIView animateWithDuration:0.25 animations:^{
        [_customView setCenter:self.center];
    }];
}

-(void)hide{
    if([self isShowing]){
        [self hideWithAnim:YES];
    }
}

-(BOOL)isShowing
{
    return self.alpha == 1.0 ? YES : NO;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self hideWithAnim:YES];
}

@end
